package com.yueya.api.assess;

import java.util.HashMap;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.aop.Duang;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.yueya.business.assess.model.Assess;
import com.yueya.business.assess.model.AssessContent;
import com.yueya.business.assess.model.SeniorHealth;
import com.yueya.business.assess.model.SeniorIllness;
import com.yueya.business.senior.model.SeniorBase;
import com.yueya.business.senior.model.SeniorGuardian;
import com.yueya.common.constant.CodeConst;
import com.yueya.common.model.Resp;
import com.yueya.common.service.BaseService;
import com.yueya.common.util.UUIDGenerator;
import net.sf.json.JSONObject;

public class IAssessService extends BaseService {

	/**
	 * 获取列表
	 * 
	 * @param jo
	 * @return
	 */
	public Resp getPageList(JSONObject jo) {
		if (containsKey(jo, "pgn", "pgs")) {

			if (jo.containsKey("pgn")) {
				pageNumber = jo.getInt("pgn");
			}
			if (jo.containsKey("pgs")) {
				pageSize = jo.getInt("pgs");
			}
			Page<Record> page = Assess.dao.paginate(pageNumber, pageSize, jo);
			if (page != null) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setData(page);
			} else {
				resp.setCode(CodeConst.WARN);
				resp.setMsg("no data");
			}
		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("参数错误");
		}
		return resp;
	}

	// 获取基本信息
	public Resp getAssessSeniorBaseDetail(JSONObject jo) {
		if (containsKey(jo, "accessid")) {
			String id = jo.getString("accessid");
			Record r = Assess.dao.getAssessSeniorBaseDetail(id);
			if (r != null) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setData(r);
			} else {
				resp.setCode(CodeConst.WARN);
				resp.setMsg("no data");
			}
		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("参数错误");
		}
		return resp;
	}

	// 获取健康档案信息
	public Resp getAssessSeniorHealthDetail(JSONObject jo) {
		if (containsKey(jo, "accessid")) {
			String id = jo.getString("accessid");
			Record r = Assess.dao.getAssessSeniorHealthDetail(id);
			if (r != null) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setData(r);
			} else {
				resp.setCode(CodeConst.WARN);
				resp.setMsg("no data");
			}

		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("参数错误");
		}
		return resp;
	}

	// 获取病例信息
	public Resp getAssessSeniorIllnessDetail(JSONObject jo) {
		if (containsKey(jo, "accessid")) {
			String id = jo.getString("accessid");
			Record r = Assess.dao.getAssessSeniorIllnessDetail(id);
			if (r != null) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setData(r);
			} else {
				resp.setCode(CodeConst.WARN);
				resp.setMsg("no data");
			}
		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("参数错误");

		}
		return resp;
	}

	// 获取评估内容
	public Resp getAssessContentDetail(JSONObject jo) {
		if (containsKey(jo, "accessid")) {
			String id = jo.getString("accessid");
			Record r = Assess.dao.getAssessContentDetail(id);
			if (r != null) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setData(r);
			} else {
				resp.setCode(CodeConst.SUCCESS);
			}

		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("参数错误");

		}
		return resp;
	}

	/**
	 * 保存基本信息
	 * 
	 * @param jo
	 * @return
	 */
	@Before(Tx.class)
	public Resp saveSeniorBase(JSONObject jo) {
		if (containsKey(jo, "name", "idcard")) {
			String idcard = jo.getString("idcard");
			Map<String, Object> queryMap = new HashMap<String, Object>();
			queryMap.put("idcard", idcard);
			SeniorBase seniorBase = SeniorBase.dao.findByMap(queryMap);
			if (seniorBase != null) {
				resp.setCode("50000");
				resp.setMsg("老人信息已存在");
			} else {
				String name = jo.getString("name");
				String gender = jo.getString("gender");
				String birthdate = jo.getString("birthdate");
				String accesstype = jo.getString("accesstype");
				String seniorid = UUIDGenerator.getUUID();
				SeniorBase sb = Duang.duang(SeniorBase.class);
				sb.set("senior_id", seniorid);
				sb.set("senior_name", name);
				sb.set("birth_date", birthdate);
				sb.set("id_card", idcard);
				sb.set("gender", gender);
				
				if (jo.containsKey("address")) {
					sb.set("address", jo.containsKey("address"));
				}
				Assess as = Duang.duang(Assess.class);
				String accessid = UUIDGenerator.getUUID();
				as.set("access_id", accessid);
				as.set("access_type", accesstype);
				boolean bl = SeniorBase.dao.save(sb);
				boolean b2 = Assess.dao.save(as);
				if (bl && b2) {
					resp.setCode(CodeConst.SUCCESS);
					resp.setMsg("保存成功");
				} else {
					resp.setCode(CodeConst.ERROR);
					resp.setMsg("保存失败");
				}
			}
		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}

		return resp;
	}

	/**
	 * 保存健康档案
	 * 
	 * @param jo
	 * @return
	 */
	@Before(Tx.class)
	public Resp saveOrUpdateSeniorHealth(JSONObject jo) {
		if (containsKey(jo, "accessid", "seniorid")) {
			// String seniorhealthid= UUIDGenerator.getUUID();
			SeniorHealth sh = Duang.duang(SeniorHealth.class);
			sh.set("access_id", jo.getString("accessid"));
			sh.set("senior_id", jo.getString("seniorid"));
			sh.set("presentillness_history", jo.getString("presentillnesshistory"));
			sh.set("beforeillness_history", jo.getString("beforeillnesshistory"));
			sh.set("medical_history", jo.getString("medicalhistory"));
			sh.set("health_reason", jo.getString("healthreason"));
			sh.set("self_medical", jo.getString("selfmedical"));
			sh.set("temperature", jo.getString("temperature"));
			sh.set("sphygmus", jo.getString("sphygmus"));
			sh.set("breath", jo.getString("breath"));
			sh.set("bloodpressure", jo.getString("bloodpressure"));
			sh.set("heartrate", jo.getString("heartrate"));
			sh.set("height", jo.getString("height"));
			sh.set("weight", jo.getString("weight"));
			sh.set("hearing", jo.getString("hearing"));
			sh.set("tooth", jo.getString("tooth"));
			sh.set("vision", jo.getString("vision"));
			sh.set("contagion", jo.getString("contagion"));
			sh.set("operaction_history", jo.getString("operactionhistory"));
			sh.set("allergy_history", jo.getString("allergyhistory"));
			sh.set("family_history", jo.getString("allergyhistory"));
			sh.set("smoke_history", jo.getString("smokehistory"));
			sh.set("smoke_old", jo.getString("smokeold"));
			sh.set("smoke_num", jo.getString("smokenum"));
			sh.set("drink_history", jo.getString("drinkhistory"));
			sh.set("drink_old", jo.getString("drinkold"));
			sh.set("drink_amount", jo.getString("drinkamount"));
			sh.set("hobby", jo.getString("hobby"));
			sh.set("taste", jo.getString("taste"));
			sh.set("taste_txt", jo.getString("tastetxt"));
			sh.set("cognize", jo.getString("cognize"));
			sh.set("cognize_txt", jo.getString("cognizetxt"));
			sh.set("emotion", jo.getString("emotion"));
			sh.set("emotion_txt", jo.getString("emotiontxt"));

			String seniorhealthid = jo.getString("seniorhealthid");
			boolean bl = false;
			if (seniorhealthid != null && seniorhealthid.length() > 0) {
				sh.set("senior_health_id", seniorhealthid);
				bl = SeniorHealth.dao.update(sh);
			} else {
				sh.set("senior_health_id", UUIDGenerator.getUUID());
				bl = SeniorHealth.dao.save(sh);
			}
			if (bl) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setMsg("保存成功");
			} else {
				resp.setCode(CodeConst.ERROR);
				resp.setMsg("保存失败");
			}
		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}

		return resp;
	}

	/**
	 * 保存病例
	 * 
	 * @param jo
	 * @return
	 */
	@Before(Tx.class)
	public Resp saveOrUpdateSeniorIllness(JSONObject jo) {
		if (containsKey(jo, "accessid", "seniorid")) {

			SeniorIllness si = Duang.duang(SeniorIllness.class);

			si.set("access_id", jo.getString("accessid"));
			si.set("senior_id", jo.getString("seniorid"));
			si.set("admission_date", jo.getString("admissiondate"));
			si.set("discharge_date", jo.getString("dischargedate"));
			si.set("illness_date", jo.getString("illnessdate"));
			si.set("illness_relator", jo.getString("illnessrelator"));
			si.set("relator", jo.getString("relator"));
			si.set("reliability", jo.getString("reliability"));
			si.set("presentillness_history", jo.getString("presentillnesshistory"));
			si.set("beforeillness_history", jo.getString("beforeillnesshistory"));
			si.set("allergy_history", jo.getString("allergyhistory"));
			si.set("personage_history", jo.getString("personagehistory"));
			si.set("married_history", jo.getString("marriedhistory"));
			si.set("family_history", jo.getString("familyhistory"));

			String illnessid = jo.getString("illnessid");
			boolean bl = false;
			if (illnessid != null && illnessid.length() > 0) {
				si.set("illness_id", illnessid);
				bl = SeniorIllness.dao.update(si);
			} else {
				si.set("illness_id", UUIDGenerator.getUUID());
				bl = SeniorIllness.dao.save(si);
			}

			if (bl) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setMsg("保存成功");
			} else {
				resp.setCode(CodeConst.ERROR);
				resp.setMsg("保存失败");
			}
		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}
		return resp;
	}

	/**
	 * 保存评估内容
	 * 
	 * @param jo
	 * @return
	 */
	@Before(Tx.class)
	public Resp saveOrUpdateAssessContent(JSONObject jo) {
		if (containsKey(jo, "accessid", "seniorid")) {

			AssessContent ac = Duang.duang(AssessContent.class);
			ac.set("access_id", jo.getString("accessid"));
			ac.set("senior_id", jo.getString("seniorid"));
			ac.set("access_eat", jo.getString("accesseat"));
			ac.set("access_wash", jo.getString("accesswash"));
			ac.set("access_wear", jo.getString("accesswear"));
			ac.set("access_bath", jo.getString("accessbath"));
			ac.set("access_excrete_shit", jo.getString("accessexcreteshit"));
			ac.set("access_excrete_diss", jo.getString("accessexcretediss"));
			ac.set("access_make_stool", jo.getString("accessmakestool"));
			ac.set("access_stair", jo.getString("accessstair"));
			ac.set("access_move", jo.getString("accessmove"));
			ac.set("access_activity", jo.getString("accessactivity"));

			String assesscontentid = jo.getString("assesscontentid");
			boolean bl = false;
			if (assesscontentid != null && assesscontentid.length() > 0) {
				ac.set("assess_content_id", assesscontentid);
				bl = AssessContent.dao.update(ac);
			} else {
				ac.set("assess_content_id", UUIDGenerator.getUUID());
				bl = AssessContent.dao.save(ac);
			}
			Map<String, Object> queryMap = new HashMap<String, Object>();
			queryMap.put("accessid", jo.getString("accessid"));
			Assess as = Assess.dao.findByMap(queryMap);
			as.set("access_score", jo.getString("accessscore"));
			as.set("access_result", jo.getString("accessresult"));
			// 更新评估表
			Assess.dao.update(as);
			if (bl) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setMsg("保存成功");
			} else {
				resp.setCode(CodeConst.ERROR);
				resp.setMsg("保存失败");
			}
		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}
		return resp;
	}

	/**
	 * 保存
	 * 
	 * @param jo
	 * @return
	 */
	@Before(Tx.class)
	public Resp update(JSONObject jo) {
		if (containsKey(jo, "name", "idcard", "seniorid")) {
			String name = jo.getString("name");
			String idcard = jo.getString("idcard");
			String gender = jo.getString("gender");
			String birthdate = jo.getString("birthdate");
			String nativeplace = jo.getString("nativeplace");
			String educational = jo.getString("educational");
			String nation = jo.getString("nation");
			String politicsstatus = jo.getString("politicsstatus");
			String marriedstatus = jo.getString("marriedstatus");
			String phonenumber = jo.getString("phonenumber");
			String foodfeature = jo.getString("foodfeature");
			String selftype = jo.getString("selftype");
			String seniortype = jo.getString("seniortype");
			String address = jo.getString("address");
			String seniornote = jo.getString("seniornote");
			String healthnote = jo.getString("healthnote");
			String nursingstandardsid = jo.getString("nursingstandardsid");
			String nursingnote = jo.getString("nursingnote");
			String bunkid = jo.getString("bunkid");
			String bodystatus = jo.getString("bodystatus");
			String guardianname = jo.getString("guardian_name");
			String guardianidcard = jo.getString("guardian_id_card");
			String guardianphone = jo.getString("guardian_phone");
			String relation = jo.getString("relation");
			String company = jo.getString("company");

			String seniorid = jo.getString("seniorid");
			
			SeniorBase sb = Duang.duang(SeniorBase.class);
			sb.set("senior_id", seniorid);
			sb.set("name", name);
			sb.set("id_card", idcard);
			sb.set("gender", gender);
			sb.set("birth_date", birthdate);
			sb.set("native_place", nativeplace);
			sb.set("educational", educational);
			sb.set("nation", nation);
			sb.set("politics_status", politicsstatus);
			sb.set("married_status", marriedstatus);
			sb.set("phone_number", phonenumber);
			sb.set("bodystatus", bodystatus);
			sb.set("food_feature", foodfeature);
			sb.set("self_type", selftype);
			sb.set("senior_type", seniortype);
			sb.set("address", address);
			sb.set("senior_note", seniornote);
			sb.set("health_note", healthnote);
			sb.set("nursing_standards_id", nursingstandardsid);
			sb.set("nursing_note", nursingnote);
			sb.set("bunk_id", bunkid);
			boolean bl = SeniorBase.dao.update(sb);

			SeniorGuardian.dao.deleteBySeniorId(seniorid);
			SeniorGuardian sg = Duang.duang(SeniorGuardian.class);
			sg.set("guardian_id", UUIDGenerator.getUUID());
			sg.set("senior_id", seniorid);
			sg.set("guardian_name", guardianname);
			sg.set("guardian_id_card", guardianidcard);
			sg.set("guardian_phone", guardianphone);
			sg.set("relation", relation);
			sg.set("company", company);
			sg.set("del_flag", "0");

			boolean b2 = sg.save();
			if (bl && b2) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setMsg("保存成功");
			} else {
				resp.setCode(CodeConst.ERROR);
				resp.setMsg("保存失败");
			}

		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}

		return resp;
	}

	/**
	 * 删除
	 * 
	 * @param jo
	 * @return
	 */
	@Before(Tx.class)
	public Resp delete(JSONObject jo) {
		if (containsKey(jo, "seniorid")) {
			String seniorid = jo.getString("seniorid");
			
			SeniorBase r = SeniorBase.dao.getDetailById(seniorid);
			r.set("del_flag", "1");
			boolean b1 = SeniorBase.dao.update(r);
			SeniorGuardian.dao.deleteBySeniorId(seniorid);
			if (b1) {
				resp.setCode(CodeConst.SUCCESS);
				resp.setMsg("删除成功");
			} else {
				resp.setCode(CodeConst.ERROR);
				resp.setMsg("删除失败");
			}

		} else {
			resp.setCode(CodeConst.JSONFORMAT_WRONG);
			resp.setMsg("JSON参数不合法");
		}

		return resp;
	}

}
